package ciyuan.Item.SkillBook;

import ciyuan.Entity.MagicBall.MagicBallEntity;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.EntityPredicate;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import java.util.List;

/**
 * @author 150149
 */
public class RangeBall extends SkillBookType {

    private static final EntityPredicate PARTNER_TARGETING = (new EntityPredicate()).range(5.0D);

    public RangeBall() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        if (playerEntity.level.isClientSide) {
            return;
        }
        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    List<LivingEntity> livingEntityList = playerEntity.level.getNearbyEntities(LivingEntity.class,PARTNER_TARGETING,playerEntity,
                            new AxisAlignedBB(playerEntity.getX()+5,playerEntity.getY()+5,playerEntity.getZ()+5,playerEntity.getX()-5,playerEntity.getY()-5,playerEntity.getZ()-5));
                    for (LivingEntity livingEntity:livingEntityList) {
                        Vector3d vector3d2 = playerEntity.getViewVector(1.0F);
                        double d6 = playerEntity.getX() - vector3d2.x * 1.0D;
                        double d7 = playerEntity.getY(0.5D) + 0.5D;
                        double d8 = playerEntity.getZ() - vector3d2.z * 1.0D;
                        double d9 = livingEntity.getX() - d6;
                        double d10 = livingEntity.getY(0.5D) - d7;
                        double d11 = livingEntity.getZ() - d8;
                        MagicBallEntity magicBallEntity1 = new MagicBallEntity(playerEntity, d9, d10, d11,playerEntity.level);
                        magicBallEntity1.moveTo(playerEntity.getX(),playerEntity.getY() + 1,playerEntity.getZ(),0.0F, 0.0F);
                        magicBallEntity1.setOwner(playerEntity);
                        SkillUtils.addEntity(magicBallEntity1, playerEntity.level);
                        playerEntity.level.playSound(null, playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), SoundEvents.BLAZE_SHOOT, playerEntity.getSoundSource(), 1.0F, 1.0F);
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ignored) { }
                }
            }
        });
    }

    @Override
    public String getSkillCastName() {
        return "RangeBall";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.5");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.5");
    }
}
